package com.devin.service.Impl;

import com.devin.dao.RoleMapper;
import com.devin.dao.UserMapper;
import com.devin.pojo.Role;
import com.devin.pojo.UserInfo;
import com.devin.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo user = userMapper.getUser(username);
        List<Role> roles = user.getRoles();
//        User user1 = new User(user.getUsername(),"{noop}"+user.getPassword(), getAuthorites());
        User user2 = new User(user.getUsername(), user.getPassword(), user.getStatus()==1?true:false, true, true,true, getAuthorites(roles));
        return user2;
    }
   /*public List<SimpleGrantedAuthority> getAuthorites(){
           //模拟用户，不去数据库中查询角色
       List<SimpleGrantedAuthority> arrayList = new ArrayList<>();
       SimpleGrantedAuthority role_user = new SimpleGrantedAuthority("ROLE_USER");
       arrayList.add(role_user);
       return  arrayList;

   }*/

    /*数据库中查询角色*/
    public List<SimpleGrantedAuthority> getAuthorites(List<Role> roles){
        List<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
        for (Role role:roles){
               simpleGrantedAuthorities.add(new SimpleGrantedAuthority( "ROLE_"+role.getRoleName()));
        }
        return simpleGrantedAuthorities;
    }

    /*查询所有用户，未查询用户对应的角色*/
    @Override
    public List<UserInfo> getUsers(int page,int size) {
        PageHelper.startPage(page,size);
        return userMapper.getUsers();
    }


    /*删除用户*/
    @Override
    public void delUser(int userId) {
        userMapper.delUserAndRole(userId);
        userMapper.delUser(userId);
    }

    /*添加用户*/
    @Override
    public void addUser(UserInfo user,Integer[] roleIds) {
        /*密码加密，存到数据库*/
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        userMapper.addUser(user);
        for(int each:roleIds){
            userMapper.addUserAndRole(user.getId(),each);
        }
    }

    @Override
    public UserInfo getUsersDetail(int userId) {
        UserInfo users = userMapper.getUsersDetail(userId);
        return users;
    }

    @Override
    public void addRoles(Integer userId,Integer[] roleIds) {
        for(Integer each:roleIds){
            userMapper.addUserAndRole(userId,each);
        }
    }

    @Override
    public void delRoleByUserId(Integer userId, Integer roleId) {
        userMapper.delRoleByUserId(userId,roleId);
    }

}
